﻿#include "ui_setTime.h"
#include "ui_ui_settime.h"
#include <QTime>

UiSetTime::UiSetTime(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::UiSetTime)
{
    ui->setupUi(this);

    QTimer * timestamp = new QTimer(this);
    connect(timestamp, &QTimer::timeout, this, &UiSetTime::refrTimestamp);
    timestamp->start(200);
}

UiSetTime::~UiSetTime()
{
    qDebug() << "析构！ UiSetTime！";
    delete ui;
}

void UiSetTime::refrTimestamp()
{
     QDateTime time = QDateTime::currentDateTime();
     QString timeStr = time.toString("yyyy-MM-dd hh:mm:ss");
     ui->timestamp->setText(timeStr);
}

void UiSetTime::on_bntSync_clicked()
{
    emit setTime(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
}

void UiSetTime::on_btnSet_clicked()
{
    QString timeStr = ui->timeInput->text();
    qDebug() << "timeStr: " << timeStr;
    QDateTime time = QDateTime::fromString(timeStr, "yyyy-M-d h:m:s");
    if (!time.isValid())
    {
        QMessageBox::warning(this,
                             "设置错误",
                             "设置失败，输入的时间格式不正确！\n"
                             "（例如: 2023-12-05 15:30:28）");
        return;
    }
    emit setTime(timeStr);
}
